/**  
* @Title: CrShopPicService.java
* @Package com.lemon.service.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年11月20日
* @version V1.0  
*/
package com.lemon.service.cityRun;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.mapper.cityRun.CrShopGoodsMapper;
import com.lemon.mapper.cityRun.crShopPicMapper;
import com.lemon.mapper.cityRun.crShopPicTypeMapper;
import com.lemon.model.cityRun.CrShopGoods;
import com.lemon.model.cityRun.crShopPic;
import com.lemon.model.cityRun.crShopPicType;
import com.lemon.utils.StringUtil;

/**
 * <p>Title: CrShopPicService</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年11月20日
 * @版本 V 1.0 
 */
@Service
public class CrShopPicServiceImpl implements CrShopPicService {
	@Resource
	private crShopPicMapper picMapper;
	@Resource
	private crShopPicTypeMapper ptMapper;
	@Resource
	private CrShopGoodsMapper goMapper;

	/**
	 * <p>
	 * Title: selectPicTypeListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param pt
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月20日
	 */
	@Override
	public List<crShopPicType> selectPicTypeListBySelective(crShopPicType pt) throws Exception {
		PageHelper.startPage(pt.getPageNum() , pt.getPageSize());
		return ptMapper.selectListBySelective(pt);
	}

	/**
	 * <p>
	 * Title: insertPicTypeBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param pt
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月20日
	 */
	@Override
	public void insertPicTypeBySelective(crShopPicType pt) throws Exception {
		ptMapper.insertSelective(pt);
	}

	/**
	 * <p>
	 * Title: updatePicTypeBySelective
	 * </p>
	 * 修改相册
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param pt
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月20日
	 */
	@Override
	public void updatePicTypeBySelective(crShopPicType pt) throws Exception {
		ptMapper.updateByPrimaryKeySelective(pt);
	}

	/**
	 * <p>
	 * Title: selectPicListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param pic
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月20日
	 */
	@Override
	public List<crShopPic> selectPicListBySelective(crShopPic pic) throws Exception {
		PageHelper.startPage(pic.getPageNum() , pic.getPageSize());
		return picMapper.selectListBySelective(pic);
	}

	/**
	 * <p>Title: updatePicBySelective</p>
	 * <p>Description:(一句话阐述): 
	 * </p>
	 * @param pt
	 * @author Ran_cc 
	 * @date 2018年11月20日
	 */
	@Override
	public void updatePicBySelective(crShopPic pt) throws Exception {
		picMapper.updateByPrimaryKeySelective(pt);
	}

	/**
	 * <p>
	 * Title: insertPicBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param pic
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月20日
	 */
	@Override
	public void insertPicBySelective(crShopPic pic) throws Exception {
		picMapper.insertSelective(pic);
	}

	/**
	 * <p>
	 * Title: selectPicAuditListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param pic
	 * @return
	 * @author Ran_cc
	 * @date 2019年3月21日
	 */
	@Override
	public List<crShopPic> selectPicAuditListBySelective(crShopPic pic) throws Exception {
		PageHelper.startPage(pic.getPageNum() , pic.getPageSize());
		return picMapper.selectPicAuditListBySelective(pic);
	}

	/**
	 * <p>
	 * Title: updataAuditPic
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param ids
	 * @param status
	 * @author Ran_cc
	 * @date 2019年3月21日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public void updataAuditPic(String ids , int status) throws Exception {
		/*
		 * if (StringUtil.isNotBlank(ids)) {
		 * String[] split = ids.split(",");
		 * for (int i = 0; i < split.length; i++) {
		 * String id = split[i];
		 * go.setId(id);
		 * goService.updateBySelective(go);
		 * }
		 * reqResult.setReturnResult("1");
		 * return reqResult;
		 * }
		 */
		//修改图片状态
		crShopPic pic = null;
		CrShopGoods go = null;
		String idss = "";
		if (StringUtil.isNotBlank(ids)) {
			String[] split = ids.split(",");
			for (int i = 0; i < split.length; i++) {
				long l = IdTypeHandler.decode(split[i]);
				if (i == 0) {
					idss = l + "";
				} else {
					idss += "," + l;
				}
			}
		}
		System.out.println(idss);
		pic = new crShopPic();
		pic.setWhere(" and a.id in ( ");
		pic.setWhereAppend(idss);
		pic.setWhereAppend(" )");
		List<crShopPic> piclist = picMapper.selectListBySelective(pic);
		List<CrShopGoods> goList = new ArrayList<CrShopGoods>();
		CrShopGoods goo = null;
		for (int i = 0; i < piclist.size(); i++) {
			goo = new CrShopGoods();
			goo.setGoodsPic(piclist.get(i).getPic());
			goo.setGoodsPicArr(piclist.get(i).getPic());
			goo.setStatus(status);
			goList.add(goo);
		}
		//批量修改图片状态
		pic.setStatus(status);
		pic.setWhere(" id in ( ");
		pic.setWhereAppend(idss);
		pic.setWhereAppend(" )");
		picMapper.updateByPrimaryKeyBatch(pic);
		//批量修改商品
		goMapper.updateStatusAuditPicBatch(goList);
		System.out.println(goList.size());
		/*
		 * int i1 = 1 / 0;
		 * System.out.println(i1);
		 * if (StringUtil.isNotBlank(ids)) {
		 * String[] split = ids.split(",");
		 * for (int i = 0; i < split.length; i++) {
		 * String id = split[i];
		 * pic = new crShopPic();
		 * //修改图片
		 * pic.setId(id);
		 * //查询图片
		 * crShopPic pic2 = picMapper.selectByPrimaryKey(id);
		 * if (null != pic2) {
		 * pic.setStatus(status);
		 * picMapper.updateByPrimaryKeySelective(pic);
		 * //根据图片路径修改商品状态
		 * go = new CrShopGoods();
		 * go.setGoodsPic(pic2.getPic());
		 * go.setGoodsPicArr(pic2.getPic());
		 * go.setStatus(status);
		 * goMapper.updateStatusAuditPic(go);
		 * }
		 * }
		 * }
		 */
	}
}
